package framework;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dbmanager.DatabaseObjectManager;
import dbmanager.FoodManager;

public class Recipe {
	int id;
	long creationDate;
	String title, steps;
	boolean approved;
	ArrayList<Ingredient> ingredients;
	ArrayList<Utensil> utensils;

	public Recipe(ResultSet recipeResultSet) throws NumberFormatException,
			SQLException {
		id = Integer.parseInt(recipeResultSet.getString("id"));
		title = recipeResultSet.getString("title");
		steps = recipeResultSet.getString("steps").replaceAll("\\\\n", "\n");
		creationDate = DatabaseObjectManager.stringToDate(recipeResultSet
				.getString("creation_date"));
		approved = recipeResultSet.getString("approved").equals("1") ? true
				: false;
		steps = recipeResultSet.getString("steps");

		// compose a list of ingredients used
		ResultSet ingredientResultSet = DatabaseObjectManager
				.getDatabaseManager().sendQuery(
						"select * from recipe_uses_food_item where recipe_id="
								+ id);
		ingredients = new ArrayList<Ingredient>();
		while (ingredientResultSet.next()) {
			ingredients.add(new Ingredient(ingredientResultSet));
		}

		// compose a list of utensils used
		ResultSet utensilResultSet = DatabaseObjectManager.getDatabaseManager()
				.sendQuery(
						"select * from recipe_uses_utensil where recipe_id="
								+ id);
		utensils = new ArrayList<Utensil>();
		while (utensilResultSet.next()) {
			utensils.add(new Utensil(utensilResultSet));
		}
	}

	public Recipe(int id, String title, String steps, long creationDate,
			boolean approved, ArrayList<Ingredient> ingredients,
			ArrayList<Utensil> utensils) {
		this.id = id;
		this.title = title;
		this.steps = steps;
		this.creationDate = creationDate;
		this.approved = approved;
		this.ingredients = ingredients;
		this.utensils = utensils;
	}
	public Recipe(String title, String steps, ArrayList<Ingredient> ingredients,
			ArrayList<Utensil> utensils){
		this.title = title;
		this.steps = steps;
		this.ingredients = ingredients;
		this.utensils = utensils;
		this.approved = false;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(long creationDate) {
		this.creationDate = creationDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSteps() {
		return steps;
	}

	public void setSteps(String steps) {
		this.steps = steps;
	}

	public boolean isApproved() {
		return approved;
	}

	public void setApproved(boolean approved) {
		this.approved = approved;
	}

	public ArrayList<Ingredient> getIngredients() {
		return ingredients;
	}

	public void setIngredients(ArrayList<Ingredient> ingredients) {
		this.ingredients = ingredients;
	}

	public ArrayList<Utensil> getUtensils() {
		return utensils;
	}

	public void setUtensils(ArrayList<Utensil> utensils) {
		this.utensils = utensils;
	}

	public String getFormattedIngredientList() throws SQLException {
		String list = "<html><big>Ingredients</big><ul size=1>";
		boolean bolding;
		for (int i = 0; i < ingredients.size(); i++) {
			bolding = FoodManager.getAmount(ingredients.get(i).getFoodItem().getName()) > 0;
			list += "<li>";
			if (bolding) {
				list += "<b>";
			}

			list += ingredients.get(i).getQuantity() + " "
					+ ingredients.get(i).getMeasuringUnit() + " - "
					+ ingredients.get(i).getFoodItem().getName();

			if (bolding) {
				list += "</b>";
			}
			bolding = false;
		}
		list += "</ul><p><p><small>Bold items are items that you have in your refrigerator!</small></html>";
		return list;
	}

	public String getFormattedStepList() {
		return "<html><big>Steps</big><ul><li>" + steps.replaceAll("\\\\n", "<li>") + "</ul></html>";
	}

	public String toString() {
		String text = "";
		text += "--" + title + " (id:" + id + ")--\n";
		text += (approved ? "Approved" : "Not Approved") + "\n\nDIRECTIONS\n"
				+ steps;
		text += "\n\nINGREDIENTS\n";

		for (int i = 0; i < ingredients.size(); i++) {
			text += ingredients.get(i).toString() + "\n";
		}

		// String.format("--%1$s (%2$d)--\n%3s", title, id, steps);
		return text;
	}
}
